package com.abu.crms.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.abu.crms.config.common.Resp;
import com.abu.crms.pojo.Classroom;
import com.abu.crms.pojo.Course;
import com.abu.crms.service.ClassroomService;
import com.abu.crms.service.CourseService;
import com.abu.crms.utils.PageUtils;
import com.abu.crms.vo.QueryConditionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 教室资源表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/classroom")
public class ClassroomController {

    @Resource
    private ClassroomService classroomService;

    @GetMapping("getClassroomListByCondition")
    public Resp getClassroomListByCondition(QueryConditionVO queryCondition) {
        PageUtils pageUtils = this.classroomService.queryPageByCondition(queryCondition);
        return Resp.ok().data("pageResult", pageUtils);
    }

    @GetMapping("getClassroomOptions")
    public Resp queryAllCourse() {
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("classroom_id", "classroom_name").eq("status", 1);
        List<Classroom> classroomList = this.classroomService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(classroomList)) {
            return Resp.ok().data("options", classroomList);
        }
        return Resp.error().msg("数据为空");
    }

    @GetMapping("getClassroom/{id}")
    public Resp getClassroomById(@PathVariable("id") Long id) {
        Classroom classroom = this.classroomService.getClassroomById(id);
        if (ObjectUtil.isNotNull(classroom)) {
            return Resp.ok().data("classroom", classroom);
        }
        return Resp.error().msg("教室资源不存在");
    }

    @PostMapping("addClassroom")
    public Resp addClassroom(@RequestParam("classroomName") String classroomName,
                             @RequestParam("type") String type,
                             @RequestParam("status")Boolean status) {
        Boolean flag = this.classroomService.addClassroom(classroomName, type,status);
        if (flag) {
            return Resp.ok().msg("添加成功");
        }
        return Resp.error().msg("添加失败");
    }

    @PutMapping("modifiedClassroom/{classroomId}")
    public Resp modifiedClassroom(@PathVariable("classroomId") Long id,
                                  @RequestParam("classroomName") String classroomName,
                                  @RequestParam("type") String type) {
        Boolean flag = this.classroomService.modifiedClassroomResource(id, classroomName, type);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @PutMapping("modifiedClassroomStatus")
    public Resp modifiedClassroomStatus(@RequestParam("classroomId") Long id,
                                        @RequestParam("status") Boolean status
    ) {
        Boolean flag = this.classroomService.modifiedClassroomStatus(id, status);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }


    @DeleteMapping("deleteByClassroomId/{id}")
    public Resp deleteById(@PathVariable("id") Long id) {
        boolean flag = this.classroomService.removeById(id);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("删除成功");
    }

}

